package com.sunchangpeng.server.bio.basic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class TimeServer {
    private static final Logger LOGGER = LoggerFactory.getLogger(TimeServer.class);

    private static final int PORT = 8080;

    public void start() throws IOException {
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(PORT);

            LOGGER.info("time server start in port:{}", PORT);

            while (!Thread.interrupted()) {
                Socket socket = serverSocket.accept();
                new Thread(new TimeServerHandler(socket)).start();
            }
        } finally {
            if (serverSocket != null) {
                serverSocket.close();
                LOGGER.info("time server close.");
                serverSocket = null;
            }
        }
    }
}
